<?php
/*
 * @Author: an
 * @Date: 2021/12/13
 * @Description: 水印管理
 */


namespace App\Services\Tencent\Live;


use App\Services\Tencent\BaseService;
use TencentCloud\Common\Credential;
use TencentCloud\Common\Profile\ClientProfile;
use TencentCloud\Common\Profile\HttpProfile;
use TencentCloud\Common\Exception\TencentCloudSDKException;
use TencentCloud\Live\V20180801\LiveClient;
use TencentCloud\Live\V20180801\Models\AddLiveWatermarkRequest;
use TencentCloud\Live\V20180801\Models\DeleteLiveWatermarkRequest;
use TencentCloud\Live\V20180801\Models\CreateLiveWatermarkRuleRequest;
use TencentCloud\Live\V20180801\Models\DescribeLiveWatermarkRulesRequest;
use TencentCloud\Live\V20180801\Models\DescribeLiveWatermarkRequest;
use TencentCloud\Live\V20180801\Models\DescribeLiveWatermarksRequest;
use TencentCloud\Live\V20180801\Models\UpdateLiveWatermarkRequest;
use TencentCloud\Live\V20180801\Models\DeleteLiveWatermarkRuleRequest;

class LiveWatermark extends BaseService
{
    /**
     * 添加水印
     * @param $PictureUrl 必填 水印图片 URL。
     * URL中禁止包含的字符：
     * ;(){}$>`#"'|
     * @param $XPosition 非必填 显示位置，X轴偏移，单位是百分比，默认 0。
     * @param $YPosition 非必填 显示位置，Y轴偏移，单位是百分比，默认 0。
     * @param $WatermarkName 必填 水印名称。
     * 最长16字节。
     * @param $Width 非必填 水印宽度，占直播原始画面宽度百分比，建议高宽只设置一项，另外一项会自适应缩放，避免变形。默认原始宽度。
     * @param $Height 非必填 水印高度，占直播原始画面高度百分比，建议高宽只设置一项，另外一项会自适应缩放，避免变形。默认原始高度。
     * @return array
     * @author an
     * @time 2021-12-13 16:49
     */
    public function addWatermark($PictureUrl, $WatermarkName, $XPosition = null, $YPosition = null, $Width = null, $Height = null)
    {
        try {

            $cred = new Credential($this->secretId, $this->secretKey);
            $httpProfile = new HttpProfile();
            $httpProfile->setEndpoint("live.tencentcloudapi.com");

            $clientProfile = new ClientProfile();
            $clientProfile->setHttpProfile($httpProfile);
            $client = new LiveClient($cred, "", $clientProfile);

            $req = new AddLiveWatermarkRequest();

            $params = array(
                "PictureUrl" => $PictureUrl,
                "XPosition" => $XPosition,
                "YPosition" => $YPosition,
                "WatermarkName" => $WatermarkName,
                "Width" => $Width,
                "Height" => $Height
            );
            $req->fromJsonString(json_encode($params));

            $resp = $client->AddLiveWatermark($req);

            return ['code' => 0, 'msg' => 'ok', 'data' => $resp];
        } catch (TencentCloudSDKException $e) {
            return ['code' => 1, 'msg' => $e->getMessage()];
        }
    }

    /**
     * 更新水印
     * @param $WatermarkId 必填 水印 ID。
     * 在添加水印接口 AddLiveWatermark 调用返回值中获取水印 ID。
     * @param $PictureUrl 必填 水印图片 URL。
     * URL中禁止包含的字符：
     * ;(){}$>`#"\'|
     * @param $XPosition 必填 显示位置，X轴偏移，单位是百分比，默认 0。
     * @param $YPosition 必填 显示位置，Y轴偏移，单位是百分比，默认 0。
     * @param $WatermarkName 非必填 水印名称。
     * 最长16字节。
     * @param $Width 非必填 水印宽度，占直播原始画面宽度百分比，建议高宽只设置一项，另外一项会自适应缩放，避免变形。默认原始宽度。
     * @param $Height 非必填 水印高度，占直播原始画面宽度百分比，建议高宽只设置一项，另外一项会自适应缩放，避免变形。默认原始高度。
     * @return array
     * @author an
     * @time 2021-12-16 16:26
     */
    public function updateLiveWatermark($WatermarkId, $PictureUrl, $XPosition, $YPosition, $WatermarkName = null, $Width = null, $Height = null)
    {
        try {

            $cred = new Credential($this->secretId, $this->secretKey);
            $httpProfile = new HttpProfile();
            $httpProfile->setEndpoint("live.tencentcloudapi.com");

            $clientProfile = new ClientProfile();
            $clientProfile->setHttpProfile($httpProfile);
            $client = new LiveClient($cred, "", $clientProfile);

            $req = new UpdateLiveWatermarkRequest();

            $params = array(
                "WatermarkId" => $WatermarkId,
                "PictureUrl" => $PictureUrl,
                "XPosition" => $XPosition,
                "YPosition" => $YPosition,
                "WatermarkName" => $WatermarkName,
                "Width" => $Width,
                "Height" => $Height
            );
            $req->fromJsonString(json_encode($params));

            $resp = $client->UpdateLiveWatermark($req);

            return ['code' => 0, 'msg' => 'ok', 'data' => $resp];
        } catch (TencentCloudSDKException $e) {
            return ['code' => 1, 'msg' => $e->getMessage()];
        }
    }


    /**
     * 获取单个水印
     * @param $WatermarkId 必填 水印ID
     * @return array
     * @author an
     * @time 2021-12-16 16:09
     */
    public function describeLiveWatermark($WatermarkId)
    {
        try {

            $cred = new Credential($this->secretId, $this->secretKey);
            $httpProfile = new HttpProfile();
            $httpProfile->setEndpoint("live.tencentcloudapi.com");

            $clientProfile = new ClientProfile();
            $clientProfile->setHttpProfile($httpProfile);
            $client = new LiveClient($cred, "", $clientProfile);

            $req = new DescribeLiveWatermarkRequest();

            $params = array(
                "WatermarkId" => $WatermarkId
            );
            $req->fromJsonString(json_encode($params));

            $resp = $client->DescribeLiveWatermark($req);

            return ['code' => 0, 'msg' => 'ok', 'data' => $resp];
        } catch (TencentCloudSDKException $e) {
            return ['code' => 1, 'msg' => $e->getMessage()];
        }
    }

    /**
     * 查询水印列表
     * @return array
     * @author an
     * @time 2021-12-16 16:13
     */
    public function describeLiveWatermarks()
    {
        try {

            $cred = new Credential($this->secretId, $this->secretKey);
            $httpProfile = new HttpProfile();
            $httpProfile->setEndpoint("live.tencentcloudapi.com");

            $clientProfile = new ClientProfile();
            $clientProfile->setHttpProfile($httpProfile);
            $client = new LiveClient($cred, "", $clientProfile);

            $req = new DescribeLiveWatermarksRequest();

            $params = array();
            $req->fromJsonString(json_encode($params));

            $resp = $client->DescribeLiveWatermarks($req);

            return ['code' => 0, 'msg' => 'ok', 'data' => $resp];
        } catch (TencentCloudSDKException $e) {
            return ['code' => 1, 'msg' => $e->getMessage()];
        }
    }


    /**
     * 删除水印
     * @param $WatermarkId 必填 水印ID
     * @return string|\TencentCloud\Live\V20180801\Models\DeleteLiveWatermarkResponse
     * @author an
     * @time 2021-12-16 15:35
     */
    public function deleteLiveWatermark($WatermarkId)
    {
        try {

            $cred = new Credential($this->secretId, $this->secretKey);
            $httpProfile = new HttpProfile();
            $httpProfile->setEndpoint("live.tencentcloudapi.com");

            $clientProfile = new ClientProfile();
            $clientProfile->setHttpProfile($httpProfile);
            $client = new LiveClient($cred, "", $clientProfile);

            $req = new DeleteLiveWatermarkRequest();

            $params = array(
                "WatermarkId" => $WatermarkId
            );
            $req->fromJsonString(json_encode($params));

            $resp = $client->DeleteLiveWatermark($req);

            return ['code' => 0, 'msg' => 'ok', 'data' => $resp];
        } catch (TencentCloudSDKException $e) {
            return ['code' => 1, 'msg' => $e->getMessage()];
        }
    }

    /**
     * 创建水印规则
     * @param $DomainName 必填 推流域名
     * @param $AppName 必填 推流路径，与推流和播放地址中的AppName保持一致，默认为live。
     * @param $StreamName 必填 流名称。
     * @param $TemplateId 必填 水印Id，即调用AddLiveWatermark接口返回的WatermarkId。
     * @return array
     * @author an
     * @time 2021-12-16 15:45
     */
    public function createLiveWatermarkRule($DomainName, $StreamName, $TemplateId, $AppName)
    {
        try {

            $cred = new Credential($this->secretId, $this->secretKey);
            $httpProfile = new HttpProfile();
            $httpProfile->setEndpoint("live.tencentcloudapi.com");

            $clientProfile = new ClientProfile();
            $clientProfile->setHttpProfile($httpProfile);
            $client = new LiveClient($cred, "", $clientProfile);

            $req = new CreateLiveWatermarkRuleRequest();

            $params = array(
                "DomainName" => $DomainName,
                "AppName" => $AppName,
                "StreamName" => $StreamName,
                "TemplateId" => $TemplateId
            );
            $req->fromJsonString(json_encode($params));

            $resp = $client->CreateLiveWatermarkRule($req);

            return ['code' => 0, 'msg' => 'ok', 'data' => $resp];
        } catch (TencentCloudSDKException $e) {
            return ['code' => 1, 'msg' => $e->getMessage()];
        }
    }

    /**
     * 删除水印规则
     * @param $DomainName 必填 推流域名。
     * @param $StreamName 必填 流名称。
     * @param $AppName 必填 推流路径。与推流和播放地址中的 AppName 保持一致，默认为live。
     * @return array
     * @author an
     * @time 2021-12-16 16:40
     */
    public function deleteLiveWatermarkRule($DomainName, $StreamName, $AppName)
    {
        try {

            $cred = new Credential($this->secretId, $this->secretKey);
            $httpProfile = new HttpProfile();
            $httpProfile->setEndpoint("live.tencentcloudapi.com");

            $clientProfile = new ClientProfile();
            $clientProfile->setHttpProfile($httpProfile);
            $client = new LiveClient($cred, "", $clientProfile);

            $req = new DeleteLiveWatermarkRuleRequest();

            $params = array(
                "DomainName" => $DomainName,
                "AppName" => $AppName,
                "StreamName" => $StreamName
            );
            $req->fromJsonString(json_encode($params));

            $resp = $client->DeleteLiveWatermarkRule($req);

            return ['code' => 0, 'msg' => 'ok', 'data' => $resp];
        } catch (TencentCloudSDKException $e) {
            return ['code' => 1, 'msg' => $e->getMessage()];
        }
    }

    /**
     * 获取水印规则列表
     * @author an
     * @time 2021-12-16 16:03
     */
    public function describeLiveWatermarkRules()
    {
        try {

            $cred = new Credential($this->secretId, $this->secretKey);
            $httpProfile = new HttpProfile();
            $httpProfile->setEndpoint("live.tencentcloudapi.com");

            $clientProfile = new ClientProfile();
            $clientProfile->setHttpProfile($httpProfile);
            $client = new LiveClient($cred, "", $clientProfile);

            $req = new DescribeLiveWatermarkRulesRequest();

            $params = array();
            $req->fromJsonString(json_encode($params));

            $resp = $client->DescribeLiveWatermarkRules($req);

            return ['code' => 0, 'msg' => 'ok', 'data' => $resp];
        } catch (TencentCloudSDKException $e) {
            return ['code' => 1, 'msg' => $e->getMessage()];
        }
    }
}
